﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;
using MP.Core.Library;
using MP.Core.Config;

public partial class Admin_Controls_ChangeUserId : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btSave_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
            //Hremployee
            HrEmployeeEntity emObj = HrEmployeeManager.CreateInstant().GetByWindowUser(txtOldUser.Text.Trim());
            if(emObj!=null)
            {
                emObj.WindowsUser = txtNewUser.Text.Trim();
                emObj.UpdatedBy = Util.CurrentUserName;
                emObj.UpdatedDate = DateTime.Now;
                HrEmployeeManager.CreateInstant().Update(emObj);
            }
           
            //hrPosition
            HrPositionEntity poObj = HrPositionManager.CreateInstant().GetByUserID(txtOldUser.Text.Trim());
            if (poObj != null)
            {
                poObj.UserId = txtNewUser.Text.Trim();
                poObj.UpdatedBy = Util.CurrentUserName;
                poObj.UpdatedDate = DateTime.Now;
                HrPositionManager.CreateInstant().Update(poObj);
            }

            //aspnet_Users
            AspnetUsersEntity userObj = AspnetUsersManager.CreateInstant().GetByUserId(txtOldUser.Text.Trim());
            if (userObj != null)
            {
                userObj.UserName = txtNewUser.Text.Trim();
                AspnetUsersManager.CreateInstant().Update(userObj);
            }
            
            CustomValidator1.ErrorMessage = "Cập nhật thành công";
            CustomValidator1.IsValid = false;
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;
    }
}
